#!/bin/bash
#
# USAGE:
#       p baselines.adr
#
# DESCRIPTION:
#  baselines.adr is an ADORE script.  
#  It generates a single master stack baseline information using the ADORE settings. 
#
# INPUT:
#  There are no input parameters. 
#  
# OUTPUT:
#  Creates ${projectFolder}/process/${runName}/baselines
#  It runs the following DORIS steps:
#    m_readfiles, m_porbits, coarseorb
#  If GNUPLOT is installed it creates a baseline plot (eps).
#  If IMAGEMAGICK is installed it converts the plot to a raster image.
#


# we will be changing directories
# save the current dir.
CALLDIR=$PWD;
generateRandomString
tmpSetFile=${randomString}
echo "Backing up current user settings to ${tmpSetFile}"
settings save ${tmpSetFile}
# DORIS does not allow master and slave to be the same. 
# We assign slave as dummy and master as the current scene.      
stackMaster=${master};
baselinesFolder=${runFolder}/baselines
[ -d "${baselinesFolder}" ] && rm -rf "${baselinesFolder}"
settings apply -r -q slave="dummy" outputFolder="${baselinesFolder}"
mkdir -p "${outputFolder}"
cd "${outputFolder}"  
echo "Saving settings for baselines processing to ${outputFolder}/settings.set"  
settings save

for scene in `scenes include`
do
     bold "Processing ${scene}"
     settings apply -r -q master=${scene} slave="dummy"
     eval "m_readfiles; m_porbits" &> ${scene}.out
done

####Now the i12s
#Set the stack master.
settings apply -r -q master=${stackMaster}
generateRandomString;	#used in plotting
local tempFile=${randomString} #used in plotting
echo ${master} > ${tempFile}_orbits.txt

for scene in `scenes include`
do
   if [ "${master}" == "${scene}" ]; then
     continue
   fi
   bold "Processing ${master}_${scene}"
   settings apply -r -q slave=${scene} outputFolder="${baselinesFolder}"
   echo "${slave}" >> ${tempFile}_orbits.txt #used in plotting
   coarseorb &> ${scene}.out
done

#### Generate Baselines Plot.
echo "0" > ${tempFile}_perp.txt
echo "0" > ${tempFile}_temp.txt
grep Bperp ${outputFolder}/*_*.res | cut -d: -f3 | tr -s " "|cut -d" " -f2  >> ${tempFile}_perp.txt
grep Btemp ${outputFolder}/*_*.res | cut -d: -f4 | tr -s " " |cut -d" " -f2 >> ${tempFile}_temp.txt
paste ${tempFile}_temp.txt ${tempFile}_perp.txt ${tempFile}_orbits.txt > ${outputFolder}/baselines
gnuplot_baseline ${outputFolder}/baselines
if [ $? -eq 0 ]; then
  if [ -e "${outputFolder}/baselines.eps" ]; then
    echo "Temporal-Perpendicular baseline plot: ${outputFolder}/baselines.eps"
    #try to convert to raster_format
    convert "${outputFolder}/baselines.eps" "${outputFolder}/baselines.${raster_format}"
    if [ $? -ne 0 ]; then
      echo "Raster file created at: ${outputFolder}/baselines.${raster_format}"
    fi
  else
    echo "Could not create baseline plot (may be gnuplot is not available?)."
    echo "List of baselines located at: ${outputFolder}/baselines" 
  fi
  rm -rf ${tempFile}_perp.txt ${tempFile}_temp.txt ${tempFile}_orbits.txt
else
  echo "There were some errors creating the baseline plot."
  echo " Please check the baselines: ${outputFolder}/baselines"
fi
#go back to where we started
cd ${CALLDIR}
#settings apply -r master=${stackMaster} outputFolder="."
settings load ${tmpSetFile}
rm -rf ${tmpSetFile}

return 0
